//
//  UserManager.m
//  atlas
//
//  Created by 你好 on 17/1/4.
//  Copyright © 2017年 xyxNav. All rights reserved.
//

#import "UserManager.h"
#import "AFHttpTool.h"
#import "PhotoAlbum.h"
#import <MJExtension.h>
#import "ApiCache.h"
@implementation UserManager

+ (UserManager *)shareInstance
{
    static UserManager* instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[[self class] alloc] init];
    });
    
    return instance;
}

- (void)cacheAllData
{
    NSString *userID=@"13126696627";
    NSString *password=@"123456";
    
    NSDictionary *loginParams=@{@"userid":userID,@"password":password};

    
    [AFHttpTool requestWithMethod:RequestMethodTypePost url:LOGIN_URL params:loginParams success:^(id response) {
        
        if ([[response objectForKey:@"code"] integerValue]==0)
        {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                
                self.isLogin=YES;
                self.userToken=[response objectForKey:@"token"];
                self.user=[User mj_objectWithKeyValues:[response objectForKey:@"result"]];
                [ApiCache cacheForUrl:LOGIN_URL jsonData:response];
            });
        }
    } failure:^(NSError *err) {
        
    }];
    
    
    NSDictionary *params=@{@"token":self.userToken};
    
    
    
    
    [AFHttpTool requestWithMethod:RequestMethodTypePost url:GETALLALBUM_URL params:params success:^(id response) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [ApiCache cacheForUrl:GETALLALBUM_URL jsonData:response];
        });
    } failure:^(NSError *err) {
    }];
    
    [AFHttpTool requestWithMethod:RequestMethodTypePost url:GETRECOMMEND_URL params:params success:^(id response) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [ApiCache cacheForUrl:GETRECOMMEND_URL jsonData:response];
        });
    } failure:^(NSError *err) {
    }];
    
    [AFHttpTool requestWithMethod:RequestMethodTypeGet url:GETABOUT_URL params:params success:^(id response) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [ApiCache cacheForUrl:GETABOUT_URL jsonData:response];
        });
    } failure:^(NSError *err) {
    }];
}


-(void)loginWithUserId:(NSString *)userId andPassword:(NSString *)password
{
    if ([ApiCache isExitCacheForUrl:LOGIN_URL])
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            NSDictionary *response=[ApiCache getCacheForUrl:LOGIN_URL];
            self.isLogin=YES;
            self.userToken=[response objectForKey:@"token"];
            self.user=[User mj_objectWithKeyValues:[response objectForKey:@"result"]];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [[GlobalVar shareInstance]notifyEvent:EVENT_USER_LOGIN_SUCC object:self.user];
            });
        });
    }
    else
    {
        NSDictionary *params=@{@"userid":userId,@"password":password};
        [AFHttpTool requestWithMethod:RequestMethodTypePost url:LOGIN_URL params:params success:^(id response) {
            if ([[response objectForKey:@"code"] integerValue]==0)
            {
                self.isLogin=YES;
                self.userToken=[response objectForKey:@"token"];
                self.user=[User mj_objectWithKeyValues:[response objectForKey:@"result"]];
               
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    [ApiCache cacheForUrl:LOGIN_URL jsonData:response];
                });
                
                [[GlobalVar shareInstance]notifyEvent:EVENT_USER_LOGIN_SUCC object:self.user];
            }
        } failure:^(NSError *err) {
            [[GlobalVar shareInstance]notifyEvent:EVENT_GETALLALBUM_FAIL object:err];
        }];
    }
}


-(void)getAllAlbum:(BOOL)isRefresh
{
    if (isRefresh)
    {
        NSDictionary *params=@{@"token":self.userToken};
        [AFHttpTool requestWithMethod:RequestMethodTypeGet url:GETALLALBUM_URL params:params success:^(id response) {
            if ([[response objectForKey:@"code"] integerValue]==0)
            {
                NSMutableArray *albumArray=[[NSMutableArray alloc]init];
                for (NSDictionary *dict in [response objectForKey:@"result"])
                {
                    PhotoAlbum *photoAlbum=[PhotoAlbum mj_objectWithKeyValues:dict];
                    [albumArray addObject:photoAlbum];
                }
                
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    [ApiCache cacheForUrl:GETALLALBUM_URL jsonData:response];
                });
                
                [[GlobalVar shareInstance]notifyEvent:EVENT_GETALLALBUM_SUCC object:albumArray];
            }
        } failure:^(NSError *err) {
            [[GlobalVar shareInstance]notifyEvent:EVENT_GETALLALBUM_FAIL object:err];
        }];
    }
    else
    {
        if ([ApiCache isExitCacheForUrl:GETALLALBUM_URL])
        {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                
                NSDictionary *response=[ApiCache getCacheForUrl:GETALLALBUM_URL];
                NSMutableArray *albumArray=[[NSMutableArray alloc]init];
                for (NSDictionary *dict in [response objectForKey:@"result"])
                {
                    PhotoAlbum *photoAlbum=[PhotoAlbum mj_objectWithKeyValues:dict];
                    [albumArray addObject:photoAlbum];
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[GlobalVar shareInstance]notifyEvent:EVENT_GETALLALBUM_SUCC object:albumArray];
                });
            });
        }
        else
        {
            NSDictionary *params=@{@"token":self.userToken};
            [AFHttpTool requestWithMethod:RequestMethodTypeGet url:GETALLALBUM_URL params:params success:^(id response) {
                if ([[response objectForKey:@"code"] integerValue]==0)
                {
                    NSMutableArray *albumArray=[[NSMutableArray alloc]init];
                    for (NSDictionary *dict in [response objectForKey:@"result"])
                    {
                        PhotoAlbum *photoAlbum=[PhotoAlbum mj_objectWithKeyValues:dict];
                        [albumArray addObject:photoAlbum];
                    }
                    
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                        [ApiCache cacheForUrl:GETALLALBUM_URL jsonData:response];
                    });
                    
                    [[GlobalVar shareInstance]notifyEvent:EVENT_GETALLALBUM_SUCC object:albumArray];
                }
            } failure:^(NSError *err) {
                [[GlobalVar shareInstance]notifyEvent:EVENT_GETALLALBUM_FAIL object:err];
            }];
        }
    }
}


-(void)getRecommend:(BOOL)isRefresh
{
    if (isRefresh)
    {
        NSDictionary *params=@{@"token":self.userToken};
        [AFHttpTool requestWithMethod:RequestMethodTypeGet url:GETRECOMMEND_URL params:params success:^(id response) {
            
            if ([[response objectForKey:@"code"] integerValue]==0)
            {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    [ApiCache cacheForUrl:GETRECOMMEND_URL jsonData:response];
                });
                
                Recommend *recommend=[Recommend mj_objectWithKeyValues:[response objectForKey:@"result"]];
                [[GlobalVar shareInstance]notifyEvent:EVENT_GETRECOMMEND_SUCC object:recommend];
            }
          
        } failure:^(NSError *err) {
            [[GlobalVar shareInstance]notifyEvent:EVENT_GETRECOMMEND_FAIL object:err];
        }];
    }
    else
    {
        if ([ApiCache isExitCacheForUrl:GETRECOMMEND_URL])
        {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSDictionary *response=[ApiCache getCacheForUrl:GETRECOMMEND_URL];
                Recommend *recommend=[Recommend mj_objectWithKeyValues:[response objectForKey:@"result"]];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[GlobalVar shareInstance]notifyEvent:EVENT_GETRECOMMEND_SUCC object:recommend];
                });
            });
        }
        else
        {
            NSDictionary *params=@{@"token":self.userToken};
            [AFHttpTool requestWithMethod:RequestMethodTypeGet url:GETRECOMMEND_URL params:params success:^(id response) {
                if ([[response objectForKey:@"code"] integerValue]==0)
                {
                    Recommend *recommend=[Recommend mj_objectWithKeyValues:[response objectForKey:@"result"]];
                    [[GlobalVar shareInstance]notifyEvent:EVENT_GETRECOMMEND_SUCC object:recommend];
                }
                
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    [ApiCache cacheForUrl:GETRECOMMEND_URL jsonData:response];
                });
                
            } failure:^(NSError *err) {
                [[GlobalVar shareInstance]notifyEvent:EVENT_GETRECOMMEND_FAIL object:err];
            }];
        }
    }
}

- (void)getAboutInfo:(BOOL)isRefresh
{
    if (isRefresh)
    {
        NSDictionary *params=@{@"token":self.userToken};
        [AFHttpTool requestWithMethod:RequestMethodTypeGet url:GETABOUT_URL params:params success:^(id response) {
            
            if ([[response objectForKey:@"code"] integerValue]==0)
            {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    [ApiCache cacheForUrl:GETABOUT_URL jsonData:response];
                });
                
                About *about=[About mj_objectWithKeyValues:[response objectForKey:@"result"]];
                [[GlobalVar shareInstance]notifyEvent:EVENT_GETABOUTINFO_SUCC object:about];
            }
        } failure:^(NSError *err) {
            [[GlobalVar shareInstance]notifyEvent:EVENT_GETABOUTINFO_FAIL object:err];
        }];
    }
    else
    {
        if ([ApiCache isExitCacheForUrl:GETABOUT_URL])
        {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSDictionary *response=[ApiCache getCacheForUrl:GETABOUT_URL];
                About *about=[About mj_objectWithKeyValues:[response objectForKey:@"result"]];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[GlobalVar shareInstance]notifyEvent:EVENT_GETABOUTINFO_SUCC object:about];
                });
            });
        }
        else
        {
            NSDictionary *params=@{@"token":self.userToken};
            [AFHttpTool requestWithMethod:RequestMethodTypeGet url:GETABOUT_URL params:params success:^(id response) {
                if ([[response objectForKey:@"code"] integerValue]==0)
                {
                    About *about=[About mj_objectWithKeyValues:[response objectForKey:@"result"]];
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                        [ApiCache cacheForUrl:GETABOUT_URL jsonData:response];
                    });
                    [[GlobalVar shareInstance]notifyEvent:EVENT_GETABOUTINFO_SUCC object:about];
                }
            } failure:^(NSError *err) {
                [[GlobalVar shareInstance]notifyEvent:EVENT_GETABOUTINFO_FAIL object:err];
            }];
        }
    }
}




@end
